#### Curating together the tiles from each biological replicate for hMyrf(humWT/+)
##3767
install.packages("ggpubr")
library(ggplot2)
library(tidyverse)
library(readr)
library(tidyr)
library(dplyr)
library(purrr)
library(ggpubr)


#Telling R where to find the files
setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_WT_1yr_flatmounts/3767/Processed/Analysis/CSV")

#Telling R where the files for this particular mouse are located, which one it should open, that it is in CSV format, and I want R to read in the CSV file, then name the object for this file "WT_3767"
WT_3767 <- list.files(path = "/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_WT_1yr_flatmounts/3767/Processed/Analysis/CSV/", pattern = "*.csv") %>% map_df(~read_csv(.))

# Repeat of what is described above until all files are loaded for each mouse
##3770
setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_WT_1yr_flatmounts/3770/Processed/Analysis")

WT_3770 <- list.files(path = "/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_WT_1yr_flatmounts/3770/Processed/Analysis/", pattern = "*Data.csv") %>% map_df(~read_csv(.))


##3776
setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_WT_1yr_flatmounts/3776/Processed/Analysis")

WT_3776 <- list.files(path = "/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_WT_1yr_flatmounts/3776/Processed/Analysis/", pattern = "*Data.csv") %>% map_df(~read_csv(.))

##3826
setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_WT_1yr_flatmounts/3826/Processed/Analysis")

WT_3826 <- list.files(path = "/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_WT_1yr_flatmounts/3826/Processed/Analysis/", pattern = "*Data.csv") %>% map_df(~read_csv(.))

#### Curating together the tiles from each biological replicate for hMyrf(humMUT/+)

##3771
setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3771/Processed/Analysis")

MUT_3771 <- list.files(path = "/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3771/Processed/Analysis/", pattern = "*Data.csv") %>% map_df(~read_csv(.))

##3772
setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3772/Processed/Analysis")

MUT_3772 <- list.files(path = "/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3772/Processed/Analysis/", pattern = "*Data.csv") %>% map_df(~read_csv(.))

##3773
setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3773/Processed/Analysis")

MUT_3773 <- list.files(path = "/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3773/Processed/Analysis/", pattern = "*Data.csv") %>% map_df(~read_csv(.))

##3774
setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3774/Processed/Analysis")

MUT_3774 <- list.files(path = "/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3774/Processed/Analysis/", pattern = "*Data.csv") %>% map_df(~read_csv(.))

##3775
setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3775/Processed/Analysis")

MUT_3775 <- list.files(path = "/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_MUT_1yr_flatmount/3775/Processed/Analysis/", pattern = "*Data.csv") %>% map_df(~read_csv(.))

########### Adding column to split by biological replicate
rep = c("3767")
WT_3767_rep <- cbind(WT_3767, rep)

rep = c("3770")
WT_3770_rep <- cbind(WT_3770, rep)

rep = c("3776")
WT_3776_rep <- cbind(WT_3776, rep)

rep = c("3826")
WT_3826_rep <- cbind(WT_3826, rep)

rep = c("3771")
MUT_3771_rep <- cbind(MUT_3771, rep)

rep = c("3772")
MUT_3772_rep <- cbind(MUT_3772, rep)

rep = c("3773")
MUT_3773_rep <- cbind(MUT_3773, rep)

rep = c("3774")
MUT_3774_rep <- cbind(MUT_3774, rep)

rep = c("3775")
MUT_3775_rep <- cbind(MUT_3775, rep)

########### Merging all WT dataframes

WT_merge <- rbind(WT_3767_rep, WT_3770_rep, WT_3776_rep, WT_3826_rep)

MUT_merge <- rbind(MUT_3771_rep, MUT_3772_rep, MUT_3773_rep, MUT_3774_rep, MUT_3775_rep)

########### Adding genotype column
genotype = c("WT")
WT_merge_geno <- cbind(WT_merge, genotype)

genotype = c("MUT")
MUT_merge_geno <- cbind(MUT_merge, genotype)

########### Merging together WT and MUT dataframes
reshape_bybiorep <- rbind(MUT_merge_geno, WT_merge_geno)

########## Saving reshape_bybiorep as csv file
write.csv(reshape_bybiorep,file='/Users/gabriellerozumek/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/reshape_bybiorep.csv', row.names=FALSE)


########### Graphing based on genotype

setwd("~/Dropbox (University of Michigan)/Prasov Lab Content/Gabbi Data/Prasov Lab/hMYRF_Cterm_Project/Whole RPE Flat Mounts/REShAPE Images/REShAPE_Figures")

reshape_bybiorep <- read.csv("reshape_bybiorep.csv")

##Cell Area graphs 
Area <- ggplot(reshape_bybiorep, mapping = aes(y = Area, x = factor(rep, level = c('3767', '3770', '3776', '3826', '3771', '3772', '3773', '3774', '3775')))) + geom_violin(aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Cell Area")

Area <- ggplot(reshape_bybiorep, mapping = aes(y = Area, x = factor(rep, level = c('3767', '3770', '3776', '3826', '3771', '3772', '3773', '3774', '3775')))) + geom_boxplot(outlier.shape = NA, aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Cell Area") + ylim(0,500)

Area <- ggplot(reshape_bybiorep, mapping = aes(y = Area, x = factor(genotype, level = c('WT', 'MUT')))) + geom_boxplot(outlier.shape = NA, aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Cell Area") + ylim(0,500)

WT <- filter(reshape_bybiorep, reshape_bybiorep$genotype == "WT")
Area_WT <- Area_WT$Area

MUT <- filter(reshape_bybiorep, reshape_bybiorep$genotype == "MUT")
Area_MUT <- Area_MUT$Area

Area_ttest <- t.test(Area_WT, Area_MUT, paired = F)

##Neighbors graphs
neighbors <- ggplot(reshape_bybiorep, mapping = aes(y = Neighbors, x = factor(rep, level = c('3767', '3770', '3776', '3826', '3771', '3772', '3773', '3774', '3775')))) + geom_boxplot(aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Number of Neighbors")

ggplot(reshape_bybiorep, mapping = aes(x = Neighbors)) + geom_histogram(aes(fill = factor(genotype))) + xlab("# of Neighbors") + ylab("Number of Cells")

#Regular histogram looking at # of cells, plotted together
ggplot(reshape_merge, mapping = aes(Neighbors, fill = genotype)) + geom_histogram(alpha = 0.5, position = 'identity') + xlab("# of Neighbors") + ylab("Number of Cells")

#Regular histogram looking at # of cells, plotted separately******
ggplot(reshape_bybiorep, mapping = aes(Neighbors, fill = genotype)) + geom_histogram(alpha = 0.5, position = 'identity') + xlab("# of Neighbors") + ylab("Number of Cells") + facet_wrap(vars(rep))

#Density showing percent cells distributed both histograms together
ggplot(reshape_merge, mapping = aes(Neighbors, fill = genotype)) + geom_histogram(alpha = 0.5, aes(y = ..density..), position = 'identity') + xlab("# of Neighbors") + ylab("Number of Cells")

#Density showing percent cells distributed, histograms plotted separately
ggplot(reshape_bybiorep, mapping = aes(Neighbors, fill = genotype)) + geom_histogram(alpha = 0.5, aes(y = ..density..), position = 'identity') + xlab("# of Neighbors") + ylab("Proportion of Cells") + facet_wrap(vars(rep))


ggplot(reshape_merge, mapping = aes(x = Neighbors)) + geom_density(aes(fill = factor(genotype)), alpha = 0.2) + xlab("# of Neighbors") + ylab("Number of Cells")

##Aspect Ratio Graphs
AR <- ggplot(reshape_bybiorep, mapping = aes(y = AR, x = factor(rep, level = c('3767', '3770', '3776', '3826', '3771', '3772', '3773', '3774', '3775')))) + geom_violin(aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Aspect Ratio")

ggplot(reshape_bybiorep, mapping = aes(y = AR, x = factor(rep, level = c('3767', '3770', '3776', '3826', '3771', '3772', '3773', '3774', '3775')))) + geom_boxplot(outlier.shape = NA, aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Aspect Ratio") + ylim(0,3)

AR_combined <- ggplot(reshape_bybiorep, mapping = aes(y = AR, x = factor(genotype, level = c('WT', 'MUT')))) + geom_boxplot(outlier.shape = NA, aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Aspect Ratio") + ylim(0,3)

AR_WT <- WT$AR

AR_MUT <- MUT$Area

AR_ttest <- t.test(AR_WT, AR_MUT, paired = F)


##Hexagonality graphs
hexagon <- ggplot(reshape_bybiorep, mapping = aes(y = Hex_Ave, x = factor(rep, level = c('3767', '3770', '3776', '3826', '3771', '3772', '3773', '3774', '3775')))) + geom_violin(aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Hexagonality")

ggplot(reshape_bybiorep, mapping = aes(y = Hex_Ave, x = factor(rep, level = c('3767', '3770', '3776', '3826', '3771', '3772', '3773', '3774', '3775')))) + geom_boxplot(outlier.shape = NA, aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Hexagonality") + ylim(0,10)

Hex_combined <- ggplot(reshape_bybiorep, mapping = aes(y = Hex_Ave, x = factor(genotype, level = c('WT', 'MUT')))) + geom_boxplot(outlier.shape = NA, aes(fill = factor(genotype))) + xlab("Genotype") + ylab("Hexagonality") + ylim(0,10)

Hex_WT <- WT$Hex_Ave

Hex_MUT <- MUT$Hex_Ave

Hex_ttest <- t.test(Hex_WT, Hex_MUT, paired = F)

# Finding medians of each biological replicate to then use for T-tests

median(WT_3767$Hex_Ave, na.rm = T)
median(WT_3770$Hex_Ave, na.rm = T)
median(WT_3776$Hex_Ave, na.rm = T)
median(WT_3826$Hex_Ave, na.rm = T)
